/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.examples.enhancedads;

import java.net.URL;
import java.util.Calendar;

import org.apache.log4j.Logger;

public class URLParams {
  public String prodId;
  public String catId;
  public int startDate;
  public int endDate;
  public Integer startHour;
  public Integer endHour;
  public String adParam1;
  public String adParam2;
  public float adParam1Min;
  public float adParam1Max;
  public float adParam2Min;
  public float adParam2Max;
  public int sunday;
  public int monday;
  public int tuesday;
  public int wednesday;
  public int thursday;
  public int friday;
  public int saturday;
  public boolean altTextAds;
  private static final Logger log = Logger.getLogger("StarterKit");

  /**
   * URLParams simplifies the url parsing process, for the parameters used in
   * Enhanced Ads Demo app. 
   */  
  public URLParams() {
    reset();
  }

  public void reset() {
    Calendar.getInstance().getTime();
    prodId = "";
    catId = "";
    startDate = -1;
    endDate = -1;
    startHour = -1;
    endHour = -1;
    adParam1 = "";
    adParam2 = "";
    adParam1Min = -1;
    adParam1Max = -1;
    adParam2Min = -1;
    adParam2Max = -1;
    sunday = -1;
    monday = -1;
    tuesday = -1;
    wednesday = -1;
    thursday = -1;
    friday = -1;
    saturday = -1;
    altTextAds = false;
  }

  public void extract(URL url) {
    reset();

    if (url.getQuery() != null) {
      String[] params = url.getQuery().split("&");
      for (String param : params) {
        String aux[] = param.split("=");
        if (aux.length > 0) {
          String name = aux[0].toLowerCase();

          String value = "";
          if (aux.length > 1) {
            value = aux[1];
          }

          try {
            if (name.equals("aw_prodid")) {
              prodId = value;
            } else if (name.equals("aw_catid")) {
              catId = value;
            } else if (name.equals("aw_start_date")) {
              startDate = Integer.parseInt(value);
            } else if (name.equals("aw_end_date")) {            
              endDate = Integer.parseInt(value);
            } else if (name.equals("aw_start_hour")) {
              startHour = Integer.parseInt(value);
            } else if (name.equals("aw_end_hour")) {
              endHour = Integer.parseInt(value);
            } else if (name.equals("aw_adparam1") ||
                name.equals("aw_param1")) {
              adParam1 = value; 
            } else if (name.equals("aw_adparam2") ||
                name.equals("aw_param2")) {
              adParam2 = value; 
            } else if (name.equals("aw_adparam1_min") ||
                name.equals("aw_param1_min")) {
              adParam1Min = Float.parseFloat(value); 
            } else if (name.equals("aw_adparam2_min") ||
                name.equals("aw_param2_min")) {
              adParam2Min = Float.parseFloat(value); 
            } else if (name.equals("aw_adparam1_max") ||
                name.equals("aw_param1_max")) {
              adParam1Max = Float.parseFloat(value); 
            } else if (name.equals("aw_adparam2_max") ||
                name.equals("aw_param2_max")) {
              adParam2Max = Float.parseFloat(value); 
            } else if (name.equals("aw_week")) {
              String weeks[] = value.toLowerCase().split(",");
              for (String day : weeks) {
                sunday = 0;
                monday = 0;
                tuesday = 0;
                wednesday = 0;
                thursday = 0;
                friday = 0;
                saturday = 0;   
                if (day.equals("su")) {
                  sunday = 1;
                } else if (day.equals("mo")) {
                  monday = 1;
                } else if (day.equals("tu")) {
                  tuesday = 1;
                } else if (day.equals("we")) {
                  wednesday = 1;
                } else if (day.equals("th")) {
                  thursday = 1;
                } else if (day.equals("fr")) {
                  friday = 1;
                } else if (day.equals("sa")) {
                  saturday = 1;
                }  
              }
            } else if (name.equals("aw_alternative")) {
              if (value.toLowerCase().trim().substring(0, 1).equals("y")) {
                altTextAds = true;
              } else {
                altTextAds = false;
              }
            }
          } catch (NumberFormatException e) {
            log.error("Failed to process Number from URLParams: Name=" 
                + name + " Value=" + value);
          }
        }
      }
    }
  }
}